Method for storing node information of huffman tree and corresponding decoding method

ABSTRACT

A method for storing node information of a Huffman tree. The method creates an index of each node in the Huffman tree using a breadth first search (BFS) algorithm. The method further reads each node of the Huffman tree beginning from a root node according to a sequence of the index of each node, and stores node information of each node into an array of the Huffman tree.

CROSS-REFERENCE TO RELATED APPLICATIONS

The present application is a divisional application of U.S. Ser. No.12/881,134, filed on Sep. 13, 2010, the entire disclosure of which isincorporated herein by reference.

BACKGROUND

1. Technical Field

Embodiments of the present disclosure relates to data encodingtechnology, and particularly to a method for storing node information ofa Huffman tree and corresponding decoding method.

2. Description of Related Art

Currently, node information of each node in a Huffman tree is storedusing a Huffman table. The node information of each node in the Huffmantree may include a return value, a code value, and a code length. Thereturn value of each node may be obtained from the Huffman tableaccording to a specified code value of encoded data when Huffmandecoding is performed on the encoded data. However, the Huffman tableoccupies too much storage space because each node needs to store atleast three kinds of values. Therefore, an efficient method for storingnode information of the Huffman tree is desired.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a schematic diagram of some embodiments of a computer.

FIG. 2 is a flowchart of some embodiments of a method for storing nodeinformation of a Huffman tree.

FIG. 3 is an example of a Huffman table for storing node information.

FIG. 4 is an example of a Huffman tree created from the Huffman table ofFIG. 3.

FIG. 5 is an example of creating an index for each node in the Huffmantree of FIG. 4.

FIG. 6 is an example of a data structure for storing node information ofthe Huffman tree.

FIG. 7 is a flowchart of some embodiments of a method for decoding datausing an array of the Huffman tree.

DETAILED DESCRIPTION

All of the processes described below may be embodied in, and fullyautomated by, functional code modules executed by one or more generalpurpose computers or processors. The code modules may be stored in anytype of non-transitory readable medium or other storage device. Some orall of the methods may alternatively be embodied in specializedhardware. Depending on the embodiment, the non-transitory readablemedium may be a hard disk drive, a compact disc, a digital video disc,or a tape drive.

FIG. 1 is a schematic diagram of some embodiments of a computer 2. Insome embodiments, the computer 2 may include a storage device 20, astoring module 22, a decoding module 23, and a processor 24. The storagedevice 20 may be used to store data 21 (e.g., a bit stream) to bedecoded. The storing module 22 may be used to store node information ofa Huffman tree in an array of the Huffman tree, and the decoding module23 may be used to decode the data 21 using the array of the Huffmantree. A detailed description will be given in the following paragraphs.

In some embodiments, the computer 2 is electronically connected to adisplay device 1 and an input device 4. Depending on the embodiment, thedisplay device 1 may be a liquid crystal display (LCD) or a cathode raytube (CRT) display, for example.

The input device 4 is provided for selecting a bit stream to be decodedby the decoding module 23. The selected bit stream may be displayed onthe display device 1. In some embodiments, the input device 4 may be akeyboard, or a mouse.

In some embodiments, the storing module 22 and the decoding module 23comprise one or more computerized instructions that are stored in thestorage device 20. The computerized code includes instructions that areexecuted by the at least one processor 24 to provide functions formodules 22-23.

FIG. 2 is a flowchart of some embodiments of a method for storing nodeinformation of a Huffman tree.

In block S10, the storing module 22 creates an index of each node in theHuffman tree using a breadth first search (BFS) algorithm. For example,as shown in FIG. 4, a Huffman tree is created according to a Huffmantable as shown in FIG. 3. FIG. 5 is a schematic diagram of the Huffmantree including a plurality of nodes having indexes. As shown in FIG. 5,the index of each node in the Huffman tree is located on the top of thenode, and recorded with a specific form, for example, a bold and italicform, such as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12.

In block S12, the storing module 22 reads each node of the Huffman treebeginning from a root node (i.e., the first node) according to asequence of the index of each node. For example, the storing modulereads each node of the Huffman tree according to an ascending order ofthe index of each node.

In block S14, the storing module 22 stores node information of each nodeinto an array of the Huffman tree. In some embodiments, the storingmodule 22 stores the node information of each node with a first part anda second part into the array of the Huffman tree. Specifically, thestoring module 22 divides the node information of each node in theHuffman tree into a first part “Value2” and a second part “LeafBit”(refer to part A of FIG. 6), and merges a binary number of a first valueof the first part and a binary number of a second value of the secondpart of each node to acquire a merged binary number. The storing module22 stores a decimal number of the merged binary number into the array ofthe Huffman tree. In some embodiments, the first value and the secondvalue are decimal numbers.

In some embodiments, the first value of the first part and the secondvalue of the second part of the node are determined by the followingsteps. The storing module 22 first determines if the node is an internalnode or a leaf node of the Huffman tree. If the node is the internalnode, the first value of the first part of the node is equal to adifference value between an index of a left child node of the node andan index of the node, and a second value of the second part of the nodeis “1.” If the node is the leaf node, the first value of the first partof the node is equal to the return value of the node, and the secondvalue of the second part of the node is “0.” In some embodiments, astorage length of the first part of each node is 7 bits, and the storagelength of the second part of each node is 1 bit.

For example, the array of the Huffman tree of FIG. 5 may be representedas follows: char Huffman_Array [13]={3, 5, 7, −2, 2, 5, 0, 4, 3, 5, −4,−6, 6}. As shown in FIG. 5, the return value of each node is in therange of [−3, 3]. The maximum difference between the index of the leftchild node of the node and the index of the node is three. Thus, thestorage length of the first part and the second part of each node isequal to 3 bits (Value2)+1 bit (LeafBit)=4 bits. In some embodiments,the storage length of the first part and the second part of each node isallocated to be 8 bits (i.e., 1 byte).

As shown in part B of FIG. 6, the node whose index equals to 5 is theinternal node (hereinafter refer to as “node five”), the first value ofthe first part of node five=7−5=2=(0000010)₂, and the second value ofthe second part of node five is “1.” Thus, the fifth element of thearray of the Huffman tree is Huffman_Array[5]=5=(00000101)₂.

As shown in part C of FIG. 6, the node whose index equals to 10 is theleaf node (hereinafter refer to as “node ten”), the first value of thefirst part of node ten=−2=(1111110)₂, and the second value of the secondpart of node ten is “0.” Thus, the tenth element of the array of theHuffman tree is Huffman_Array[10]=−4=(11111100)₂.

FIG. 7 is a flowchart of some embodiments of a method for decoding datausing the array of the Huffman tree.

In block S20, the decoding module 23 obtains a bit stream to be decodedfrom the storage device 20.

In block S21, the decoding module 23 reads a root node (i.e., the firstnode) of the Huffman tree to decode the bit stream.

In block S22, the decoding module 23 determines if the node is a leafnode. The procedure goes to block S23 if the node is not the leaf node,or the procedure goes to block S24 if the node is a leaf node.Specifically, the decoding module 23 determines that the node is theinternal node if the second value of second part of the node is “1,” ordetermines that the node is the leaf node if the second value of secondpart of the node is “0.”

In block S23, the decoding module 23 reads a bit from the bit streamsequentially to determine a next node in the Huffman tree, and theprocedure returns to block S22 to determine if the next node is a leafnode. In some embodiments, the next node in the Huffman tree isdetermined by the following formulas. If the bit read from the bitstream is “0,” the index of the next node is equal to (the index of acurrent node+the first value of the first part of the current node). Ifthe bit read from the bit stream is “1,” the index of the next node isequal to (the index of the current node+the first value of the firstpart of the current node+1).

In block S24, the decoding module 23 searches a decimal number of thenode from the array of the Huffman tree according to an index of thenode, and obtains the return value of the node from the decimal numberof the node. In some embodiments, the return value of the node is equalto the first value of the first part of the node. Specifically, thefirst value of the first part of the node is determined by the followingsteps. The decoding module 23 converts the decimal number of the node tothe binary number, and performs a right arithmetic shift operation onthe binary number by 1 bit to obtain a right-shifted binary number. Thedecoding module 23 converts the right-shifted binary number to thedecimal number to obtain the first value of the first part of the node.

In block S25, the decoding module 23 determines if reading of the bitstream has been finished. The procedure returns to block S21 if readingof the bit stream has not been finished. Otherwise, the procedure goesto block S26 if reading of the bit stream has been finished.

In block S26, the decoding module 23 outputs the return value of eachnode to generate a decoded bit stream.

An example of decoding a bit stream of “0101000110110111” using thearray of the Huffman tree of FIG. 5 is provided as follows. The array ofthe Huffman tree of FIG. 5 may be “char Huffman_Array [13]={3, 5, 7, −2,2, 5, 0, 4, 3, 5, −4, −6, 6}.”

In step 1, the decoding module 23 reads a node of the Huffman tree beganfrom the root node. The index of the root node is “0.”

In step 2, the decoding module 23 determines if the node is the leafnode. Because a last bit of the binary number of Huffman_Array[0] is“1,” thus, the node is not the leaf node.

In step 3, the decoding module 23 reads a bit from the bit stream of“0101000110110111” sequentially. Because the first bit is “0,” the indexof the next node=(0+(Huffman Array[0]>>1))=(0+1)=1. “Huffman_Array[i]>>1” represents the first value of the first part of the node whoseindex is “i.”

In step 4, the decoding module 23 determines if the node whose indexequals to 1 is the leaf node. Because the last bit of the binary numberof Huffman_Array[1] is “1,” the node is not the leaf node.

In step 5, the decoding module 23 reads a subsequent bit from the bitstream of “0101000110110111” sequentially. Because the subsequent bit is“1,” the index of the next node=(1+(Huffman_Array[1]>>1)+1)=(1+2+1)=4.Thus, the next node is the fourth element of Huffman Array[4].

In step 6, the decoding module 23 determines if the node whose indexequals to 4 is the leaf node. Because the last bit of the binary numberof Huffman_Array[4] is “0,” that is, the node is the leaf node. Thus,the decoding module 23 obtains the return value of the node whose indexequals to 4, and the return value=Huffman_Array[4]>>1=1.

In step 7, the decoding module 23 repeats the steps 1-6 to decode theremain bits of the bit stream (i.e., 0101000110110111, the underlinedbits) until reading of the bit stream has been finished. Thus, thedecoding module 23 may obtain the decoded bit stream of {1, 1, −1, 1,−2, 0}.

It should be emphasized that the above-described embodiments of thepresent disclosure, particularly, any embodiments, are merely possibleexamples of implementations, merely set forth for a clear understandingof the principles of the disclosure. Many variations and modificationsmay be made to the above-described embodiment(s) of the disclosurewithout departing substantially from the spirit and principles of thedisclosure. All such modifications and variations are intended to beincluded herein within the scope of this disclosure and the presentdisclosure and protected by the following claims.

1. A method for decoding data using an array of a Huffman tree, nodeinformation of each node in the Huffman tree being stored in the arrayof the Huffman tree, each node comprising a return value, the methodcomprising: (a) obtaining a bit stream to be decoded; (b) reading a rootnode of the Huffman tree; (c) determining whether the node is a leafnode; (d) reading a bit from the bit stream sequentially to determine anext node in the Huffman tree when the node is not the leaf node, andreturning to step (c) to determine whether the next node is a leaf node;(e) searching a decimal number of the node from the array of the Huffmantree according to an index of the node when the node is a leaf node, andobtaining the return value of the node from the decimal number of thenode; (f) determining whether reading of the bit stream has beenfinished; (g) returning to step (b) when reading of the bit stream hasnot been finished; or (h) outputting the return value of each node togenerate a decoded bit stream when reading of the bit stream has beenfinished.
 2. The method according to claim 1, wherein the nodeinformation of each node is stored with a first part and a second partinto the array of the Huffman tree.
 3. The method according to claim 2,wherein the node information of each node are stored into the array ofthe Huffman tree by: dividing the node information of each node in theHuffman tree into a first part and a second part; merging a binarynumber of a first value of the first part and a binary number of asecond value of the second part of each node to acquire a merged binarynumber, the first value and the second value being decimal numbers;storing a decimal number of the merged binary number into the array ofthe Huffman tree.
 4. The method according to claim 3, wherein a storagelength of the first part of each node is 7 bits, and the storage lengthof the second part of each node is 1 bit.
 5. The method according toclaim 3, wherein the first value of the first part and the second valueof the second part of the node are determined by: determining whetherthe node is an internal node or a leaf node of the Huffman tree;determining that a first value of the first part of the node is equal toa difference value between an index of a left child node of the node andan index of the node when the node is the internal node, and a secondvalue of the second part of the node is “1;” or determining that thefirst value of the first part of the node is equal to the return valueof the node when the node is the leaf node, and the second value of thesecond part of the node is “0.”
 6. The method according to claim 5,wherein the step of determining whether the node is a leaf nodecomprises: determining that the node is the internal node when thesecond value of second part of the node is “1”; and determining that thenode is the leaf node when the second value of second part of the nodeis “0”.
 7. The method according to claim 5, wherein the next node in theHuffman tree in step (d) is determined by the following formulas: theindex of the next node=(the index of a current node+the first value ofthe first part of the current node), upon the condition that the bitread from the bit stream is “0”; and the index of the next node=(theindex of the current node+the first value of the first part of thecurrent node+1), upon the condition that the bit read from the bitstream is “1”.
 8. The method according to claim 7, wherein the returnvalue of the node is equal to the first value of the first part of thenode.
 9. The method according to claim 8, wherein the first value of thefirst part of the node is determined by the following steps: convertingthe decimal number of the node to the binary number, and performing aright arithmetic shift operation on the binary number by 1 bit to obtaina right-shifted binary number; and converting the right-shifted binarynumber to the decimal number to obtain the first value of the first partof the node.